/**
 * 聚合点
 * @param {Earth} options.earth 渲染容器
 * @param {Earth} options.points 海量点坐标列表
 * @param {Earth} options.url 普通点图片路径
 * @param {Earth} options.clusterUrl 聚合点图片路径
 * 
 */


function DrawClusterPoint (options) {
    
    let viewer = options.earth._viewer
    this.clusterUrl = options.clusterUrl

    // 写入普通点
    this.setPoint(options.points, options.url)
    let pointEntities = this.pointEntities

    let dataSource = new Cesium.CustomDataSource("cluster");
    viewer.dataSources.add(dataSource);
    console.log(viewer.dataSources)
    for(let i = 0;i < pointEntities.length;i++ ) {
        dataSource.entities.add(pointEntities[i])
    }

    dataSource.clustering.enabled = true;
    dataSource.clustering.pixelRange = 25;
    dataSource.clustering.minimumClusterSize = 5;
    this.dataSource = dataSource
    

    // 设置聚合点
    this.customStyle()
    this.viewer = viewer

}

DrawClusterPoint.prototype.setPoint = function (points, url) {
    let pointEntities = [];
    for (let i = 0; i < points.length; i++){
        let lng = points[i][0];
        let lat = points[i][1];
        // 添加普通点
        let entity = new Cesium.Entity({
            id: "marker" + i,
            position : Cesium.Cartesian3.fromDegrees(lng, lat),
            billboard :{
                image : url,
                width: 30,  // 宽高必须设置，否则首次无法聚合
                height: 30,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM
            }
        })
        pointEntities.push(entity);
    }

    this.pointEntities = pointEntities
}

DrawClusterPoint.prototype.customStyle = function () {
    if (Cesium.defined(this.removeListener)) {
        this.removeListener();
        this.removeListener = undefined;
    } else {
        let that = this
        this.removeListener = this.dataSource.clustering.clusterEvent.addEventListener(
            function (clusteredEntities, cluster) {
                // 聚合点显示聚合数量
                cluster.label.show = true;
                cluster.label.font = '24px sans-serif'
                cluster.label.verticalOrigin = Cesium.VerticalOrigin.BOTTOM
                cluster.label.horizontalOrigin = Cesium.VerticalOrigin.CENTER
                cluster.label.pixelOffset = new Cesium.Cartesian2(0, -22)
                cluster.label.eyeOffset = new Cesium.Cartesian3(0, 0, -100)

                // 聚合点图片
                cluster.billboard.show = true;
                cluster.billboard.id = cluster.label.id;
                cluster.billboard.verticalOrigin = Cesium.VerticalOrigin.BOTTOM;
                cluster.billboard.horizontalOrigin = Cesium.VerticalOrigin.CENTER
                cluster.billboard.width = 60;
                cluster.billboard.height = 60;
                cluster.billboard.image = that.clusterUrl;

                // clusteredEntities 聚合数组
                // clusteredEntities.length 聚合数量
                // cluster 当前聚合显示点实例
            }
        );
    }
}

DrawClusterPoint.prototype.destroy = function () {
    this.viewer.dataSources.remove(this.dataSource)
}

export default DrawClusterPoint